#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 文本编辑
    ui->lineEdit_name->setPlaceholderText("请输入账号：");
    ui->lineEdit_name->setClearButtonEnabled(true);

    ui->lineEdit_password->setPlaceholderText("请输入密码：");
    ui->lineEdit_password->setClearButtonEnabled(true);
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);//输入隐藏，用星号、圆点代替
    ui->lineEdit_password2->setPlaceholderText("请再次输入密码：");
    ui->lineEdit_password2->setClearButtonEnabled(true);
    ui->lineEdit_password2->setEchoMode(QLineEdit::Password);
    ui->label_5->setText("密码为空");

    ui->lineEdit_phone->setPlaceholderText("请输入电话：");
    ui->lineEdit_phone->setClearButtonEnabled(true);
    ui->lineEdit_phone->setInputMask("000-0000-0000");//固定格式，0代表只能是数字，只能进行简单验证

    // 基于正则表达式来设置输入验证器
    QRegExp regExp("^1\\d{10}$");// 以1开头，后面是个数字
    ui->lineEdit_phone2->setPlaceholderText("请输入电话：");
    ui->lineEdit_phone2->setClearButtonEnabled(true);
    ui->lineEdit_phone2->setValidator(new QRegExpValidator(regExp));// 设置验证器

    // 多行输入框输入内容显示到标签中
    ui->label_6->setWordWrap(true);

    // 下拉列表
    ui->comboBox->addItem("麦辣鸡腿堡");
    ui->comboBox->addItem("双层芝士牛肉堡");
    // 从文件中读取并添加到下拉列表
    ifstream file("D:/git_warehouse/Qt/widget_4/config.txt");
    if(!file.is_open())
    {
        qDebug() << "文件打开失败";
        return;
    }
    string line;
    while(getline(file,line))
    {
        // 将string转换成QString并添加到下拉列表
        ui->comboBox_2->addItem(QString::fromStdString(line));
    }
    file.close();

    // 微调控件
    ui->spinBox->setRange(1,5);// 调整范围
    ui->spinBox->setValue(1);// 初始值

    // slider拉动条
    ui->horizontalSlider->setMinimum(100);
    ui->horizontalSlider->setMaximum(2000);
    ui->horizontalSlider->setValue(800);
    ui->horizontalSlider->setSingleStep(50);

    ui->verticalSlider->setMinimum(100);
    ui->verticalSlider->setMaximum(2000);
    ui->verticalSlider->setValue(600);
    ui->verticalSlider->setSingleStep(50);

    // 设置快捷键,控制窗口的宽度
    QShortcut* shortCut1 = new QShortcut(this);
    shortCut1->setKey(QKeySequence("-"));

    QShortcut* shortCut2 = new QShortcut(this);
    shortCut2->setKey(QKeySequence("="));

    connect(shortCut1, &QShortcut::activated, this, &Widget::subValue);
    connect(shortCut2, &QShortcut::activated, this, &Widget::addValue);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::compare()
{
    const QString& s1 = ui->lineEdit_password->text();
    const QString& s2 = ui->lineEdit_password2->text();

    if(s1.isEmpty() && s2.isEmpty())
        ui->label_5->setText("密码为空");
    if(s1 == s2)
        ui->label_5->setText("密码一致");
    else
        ui->label_5->setText("密码不一致");
}
// 快捷键控制窗口宽度
void Widget::addValue()
{
    int value = ui->horizontalSlider->value();
    if (value >= ui->horizontalSlider->maximum())
        return;
    ui->horizontalSlider->setValue(value + 5);
}

void Widget::subValue()
{
    int value = ui->horizontalSlider->value();
    if (value <= ui->horizontalSlider->minimum())
        return;
    ui->horizontalSlider->setValue(value - 5);
}

// 显示输入文本信息
void Widget::on_pushButton_clicked()
{
    qDebug() << "账号：" << ui->lineEdit_name->text();
    qDebug() << "密码：" << ui->lineEdit_password->text();
    QString gender = ui->radioButton_male->isChecked() ? "男" : "女";
    qDebug() << "性别：" << gender;
    qDebug() << "电话：" << ui->lineEdit_phone->text();
}

// 利用正则表达式设置的验证器
void Widget::on_lineEdit_phone2_textEdited(const QString &text)
{
    QString content = text;
    int pos = 0;
    if(ui->lineEdit_phone2->validator()->validate(content,pos) == QValidator::Acceptable)
    {
        // 验证通过
        ui->pushButton_confirm->setEnabled(true);
        qDebug() << content;
    }
    else
    {
        // 验证失败
        ui->pushButton_confirm->setEnabled(false);
    }
}

// 验证两次密码是否输入一致
void Widget::on_lineEdit_password_textEdited(const QString &arg1)
{
    (void) arg1;
    this->compare();
}
void Widget::on_lineEdit_password2_textEdited(const QString &arg1)
{
    (void) arg1;
    this->compare();
}

// 显示密码复选框
void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Normal);
        ui->lineEdit_password2->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Password);
        ui->lineEdit_password2->setEchoMode(QLineEdit::Password);
    }
}
//文本内容改变时触发
void Widget::on_textEdit_textChanged()
{
    const QString& text = ui->textEdit->toPlainText();
    ui->label_6->setText(text);
}
// 选中内容变化时触发
void Widget::on_textEdit_selectionChanged()
{
    QTextCursor cursor = ui->textEdit->textCursor();
    qDebug() << "selectChange:" << cursor.selectedText();
}

void Widget::on_textEdit_cursorPositionChanged()
{
    QTextCursor cursor = ui->textEdit->textCursor();
    qDebug() << "cursorPositionChanged:" << cursor.position();
}

// 撤销操作是否可以执行，输入或撤销后触发
void Widget::on_textEdit_undoAvailable(bool b)
{
    qDebug() << "undoAvailable:" << b;
}
// 撤销撤销操作是否可以执行，撤销或撤销撤销后触发
void Widget::on_textEdit_redoAvailable(bool b)
{
    qDebug() << "redoAvailable:" << b;
}
// 是否选中内容可以复制，选中或取消选中内容触发
void Widget::on_textEdit_copyAvailable(bool b)
{
    qDebug() << "copeAvailable:" << b;
}

void Widget::on_pushButton_2_clicked()
{
    qDebug() << ui->comboBox_2->currentText();
    qDebug() << ui->comboBox->currentText();
    qDebug() << ui->spinBox->text();
}
// 计算时间间隔
void Widget::on_pushButton_3_clicked()
{
    QDateTime time1 = ui->dateTimeEdit->dateTime();
    QDateTime time2 = ui->dateTimeEdit_2->dateTime();
    //int days = abs(time1.daysTo(time2));// 因为不到间隔一天的两天也会算成一天，所以不用这个了
    int days = abs(time1.secsTo(time2)) / (3600 * 24);
    int secs = abs(time1.secsTo(time2)) % (3600 * 24);

    QString sdays(QString::number(days));
    QString ssecs(QString::number(secs));
    sdays += " day";
    ssecs += " s";
    ui->label_10->setText(sdays);
    ui->label_11->setText(ssecs);
}
// dial旋钮
void Widget::on_dial_valueChanged(int value)
{
    this->setWindowOpacity((double)value / 100);
}

// 通过滑动条 设置显示窗口宽高
void Widget::on_horizontalSlider_valueChanged(int value)
{
    const QRect& rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), value, rect.height());
}

void Widget::on_verticalSlider_valueChanged(int value)
{
    const QRect& rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), rect.width(), value);
}
